--- Test properties of the 'Environment' module
module tests.qc.Environment where

import System.Environment as E
import Test.QuickCheck

-- since we don't (yet) have Test.QuickCheck.Monadic we will cheat here
-- and use IO.performUnsafe since these functions doesn't actually have
-- any side effects (despite their IO types)

-- this may break once real main arguments are wired in, depending on
-- how the tests are actually invoked:
-- all we can really say is that the number of arguments is >= 0
--- nowarn: unsafe
o_getArgsEmpty = once ( length (IO.performUnsafe getArgs) >= 0 )

--- nowarn: unsafe
o_getProgNameEmpty = once ( IO.performUnsafe (getProgName) == "" )

-- PATH should be available even on Windows so this should be a safe test:
--- nowarn: unsafe
o_getEnvPathOk = once ( IO.performUnsafe (getEnv "PATH") /= "" )

-- this should yield an environment variable name that we will never have:
noSuchEnv = "NoSuchEnvironmentVariable"

-- getEnv throws IllegalArgumentException with the name of the unknown
-- environment variable as the message value:
accept :: IllegalArgumentException -> IO String
accept t = return ("IAE" ++ t.getMessage)

--- nowarn: unsafe
o_getEnvUnknownThrows = once ( IO.performUnsafe (getEnv noSuchEnv `catch` accept) == "IAE" ++ noSuchEnv )
